<?php

namespace AppBundle\Controller;

use AppBundle\Events;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use AppBundle\Entity\User;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
    {
        // replace this example code with whatever you need
        return $this->render('default/index.html.twig', [
            'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'),
        ]);
    }

    public function crumbAction($labels=[])
    {

        return $this->render('default/crumb.html.twig',[
            'labels'=>$labels,
        ]);
    }

    /**
     * @Route("/login",name="login")
     */
    public function loginAction(Request $request)
    {
        $repository = $this->getDoctrine()->getRepository('AppBundle:User');
        $user = new User();

        $user->setOptions([
            'confirmVerifyCode'=>$request->getSession()->get('verifyCode',''),
            'repository'=>$repository
        ]);


        $form = $this->createForm('AppBundle\Form\LoginType', $user);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $session = $request->getSession();
            $session->set('user',$repository->getLoginUser());
            $session->remove('verifyCode');
            return $this->redirectToRoute('index_index');
        }
        return $this->render('default/login.html.twig',[
            'form'=>$form->createView(),
        ]);
    }

    /**
     * @Route("/register",name="register")
     */
    public function registerAction(Request $request,EventDispatcherInterface $eventDispatcher)
    {
        $repository = $this->getDoctrine()->getRepository('AppBundle:User');
        $user = new User();
        $user->setOptions([
            'repository'=>$repository,
        ]);
        $form = $this->createForm('AppBundle\Form\RegisterType',$user);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $eventDispatcher->dispatch(Events::USER_BEFORE_CREATE,new GenericEvent($user));
            $em->persist($user);
            $em->flush();
            return $this->redirectToRoute('login');
        }
        return $this->render('default/register.html.twig',[
            'form'=>$form->createView()
        ]);
    }

    /**
     * @Route("/logiut",name="logout")
     */
    public function logoutAction(Request $request)
    {
        $request->getSession()->remove('user');
        return $this->redirectToRoute('login');
    }

    /**
     * @Route("/code",name="code")
     */
    public function codeAction(Request $request)
    {
        $str = "3,4,5,6,7,8,9,a,b,c,d,e,f,g";
        $list = explode(",",$str);
        $max = count($list) - 1;
        $verify = "";
        for($i = 0; $i <5;$i ++){
            $rand = mt_rand(0,$max);
            $verify .= $list[$rand];
        }
        $request->getSession()->set('verifyCode',$verify);
        $im = imagecreate(58,28);
        $black = imagecolorallocate($im,0,0,0);
        $white = imagecolorallocate($im,255,255,255);
        imagefill($im,0,0,$white);
        imagestring($im,5,10,8,$verify,$black);
        imagepng($im);
        imagedestroy($im);
        $response = new Response();
        $response->headers->set('Content-type','image/png');
        return $response;
    }
}
